package com.swxc.smartorder.app;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.HttpParams;
import com.swxc.smartorder.BuildConfig;
import com.swxc.smartorder.utils.ApkVersionCodeUtils;
import com.swxc.smartorder.utils.AppSharePreferenceMgr;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import cn.jpush.android.api.JPushInterface;
import okhttp3.OkHttpClient;

/**
 * Author   Administrator
 * Date     2018/3/29
 * Desc     eg
 *
 * @author Administrator
 */
public class App extends Application {

    private static String registrationId;

    private static Context sContext;

    public static Context getContext() {
        return sContext;
    }

    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        sContext = getApplicationContext();
        initOkGo();
        initJPush();
        initBugly();
    }

    /**
     * 初始化极光推送
     */
    private void initJPush() {
        //极光推送初始化其一
        JPushInterface.setDebugMode(true);
        JPushInterface.init(sContext);
        registrationId = JPushInterface.getRegistrationID(sContext);
        AppSharePreferenceMgr.put(sContext, ConfigSPS.REGISTRATION_ID, registrationId);
        Log.e("app", registrationId);


    }

    /**
     * 初始化网络请求
     */
    private void initOkGo() {
        HttpHeaders headers = new HttpHeaders();
        headers.put("User-Agent", "android");
        HttpParams params = new HttpParams();

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .readTimeout(3, TimeUnit.SECONDS)
                .writeTimeout(3, TimeUnit.SECONDS)
                .connectTimeout(3, TimeUnit.SECONDS)
//                .addInterceptor(new TokenInterceptor())
                .build();


        OkGo.getInstance().init(this)
                .setCacheMode(CacheMode.NO_CACHE)
                //建议设置OkHttpClient，不设置会使用默认的
                .setOkHttpClient(okHttpClient)
                //全局统一超时重连次数，默认为三次，那么最差的情况会请求4次(一次原始请求，三次重连请求)，不需要可以设置为0
                .setRetryCount(3)
                //全局公共头
                .addCommonHeaders(headers)
                //全局公共参数
                .addCommonParams(params)
        ;
    }

    /**
     * 初始化Bugly
     */
    private void initBugly() {
        Context applicationContext = getApplicationContext();
        String packageName = applicationContext.getPackageName();
        String processName = getProcessName(android.os.Process.myPid());
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(applicationContext);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
        strategy.setAppVersion(ApkVersionCodeUtils.getVerName(this));
        strategy.setAppPackageName(packageName);
        strategy.setAppReportDelay(20000);
        CrashReport.initCrashReport(getApplicationContext(), "d7e51c8262", BuildConfig.DEBUG, strategy);

    }
}
